<template>
  <el-dialog :visible.sync="show" append-to-body title="缴费窗口" :close-on-click-modal="false">
    <k-form label-width="100px" ref="form" @submit="onSubmit">
      <k-form-item label="缴费学员">
        <el-input v-model="form.realname" readonly/>
      </k-form-item>

      <k-form-item label="还需缴纳">
        <el-input :value="(form.money - form.pay_money).toFixed(2)" readonly style="width: 200px">
          <span slot="append">元</span>
        </el-input>
      </k-form-item>

      <k-form-item label="缴费金额">
        <el-input v-model="payValue" style="width: 200px">
          <span slot="append">元</span>
        </el-input>
        <el-button type="text" class="ke__margin-left" @click="onClickPayAll">全部缴清</el-button>
      </k-form-item>
    </k-form>

    <div slot="footer">
      <el-button type="primary" @click="$refs.form.onSubmit()" :loading="subing">确认</el-button>

      <el-button @click="show = false">关闭</el-button>
    </div>
  </el-dialog>
</template>

<script>
export default {
  name: 'index-pay',
  props: {
    value: Boolean
  },
  computed: {
    show: {
      get () {
        return this.value
      },
      set (val) {
        this.$emit('input', val)
      }
    }
  },
  data () {
    return {
      form: {},
      payValue: '',
      subing: false,
      callback: null
    }
  },
  methods: {
    onClickPayAll () {
      this.payValue = this.form.money - this.form.pay_money
    },
    onSubmit () {
      this.subing = true
      this.$http.post(`admin/student/pay/${this.form.id}`, {
        money: this.payValue
      })
        .finally(() => this.subing = false)
        .then(result => {
          if (result.res_code === 0) {
            this.$message.success('费用成功缴纳')
            this.callback({ ...this.form, pay_money: this.payValue })
            this.show = false
          } else {
            this.$message.error(result.res_msg)
          }
        })
        .catch(err => {
          console.log(err)
          if (err.response.status !== 404) {
            this.$message.error('缴纳失败')
          }
        })
    }
  }
}
</script>

<style scoped>

</style>
